package p0;

public class Leetcode_45 {

    public static int jump(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int reach = 0;
        int nextReach = nums[0];
        int step = 0;
        for (int i = 0; i < nums.length; i++) {
            nextReach = Math.max(i + nums[i], nextReach);
            if (nextReach >= nums.length - 1) {
                return step + 1;
            }
            if (i == reach) {
                step++;
                reach = nextReach;
            }
        }
        return step;
    }

    public static void main(String[] args) {
        System.out.println(jump(new int[]{2,3,1,1,4}));
    }
}
